package cn.sirun.com.fca.account.presenter.impl;

import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import cn.sirun.com.fca.account.constant.AccountActionConst;
import cn.sirun.com.fca.account.loader.PhoneLoader;
import cn.sirun.com.fca.account.model.base.SubmitPhoneRequest;
import cn.sirun.com.fca.account.model.base.SubmitPhoneResponse;
import cn.sirun.com.fca.account.model.register.SubmitEmailRequest;
import cn.sirun.com.fca.account.presenter.AccountFlowPresenter;
import cn.sirun.com.fca.account.presenter.IPhonePresenter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

public class PhonePresenter extends AccountFlowPresenter<PhoneLoader> implements IPhonePresenter {

    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    public void submitPhone(SubmitPhoneRequest request) {
        getLoader().submitPhone(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SubmitPhoneResponse>>() {

                    @Override
                    public void onNext(BaseResponse<SubmitPhoneResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        PhonePresenter.this.getActionListener().onAction
                                (AccountActionConst.Normal.ACTION_SUBMIT_PHONE_RESULT,
                                        submitPhoneResponse);
                    }
                });
    }

    @Override
    public void submitEmail(SubmitEmailRequest request) {
        getLoader().submitEmail(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SubmitPhoneResponse>>() {

                    @Override
                    public void onNext(BaseResponse<SubmitPhoneResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        PhonePresenter.this.getActionListener().onAction
                                (AccountActionConst.Normal.ACTION_SUBMIT_PHONE_RESULT,
                                        submitPhoneResponse);
                    }
                });
    }

    @Override
    public void resetSubmitPhone(SubmitPhoneRequest request) {
        getLoader().resetSubmitPhone(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SubmitPhoneResponse>>() {

                    @Override
                    public void onNext(BaseResponse<SubmitPhoneResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        PhonePresenter.this.getActionListener().onAction
                                (AccountActionConst.Normal.ACTION_SUBMIT_PHONE_RESULT,
                                        submitPhoneResponse);
                    }
                });
    }

    @Override
    public void resetSubmitEmail(SubmitEmailRequest request) {
        getLoader().resetSubmitEmail(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SubmitPhoneResponse>>() {

                    @Override
                    public void onNext(BaseResponse<SubmitPhoneResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        PhonePresenter.this.getActionListener().onAction
                                (AccountActionConst.Normal.ACTION_SUBMIT_PHONE_RESULT,
                                        submitPhoneResponse);
                    }
                });
    }

    @Override
    protected PhoneLoader createLoader() {
        return new PhoneLoader();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }
}
